package com.rabbitmq.exec.work;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import com.rabbitmq.exec.utils.RabbitMqUtils;
import com.rabbitmq.exec.utils.SleepUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @Description
 * @Author lhj
 * @Date 2024/4/13 21:58
 */
public class Worker04 {

    static Logger logger = LoggerFactory.getLogger(Worker04.class);

    public static final String ACK_QUEUE_NAME = "ack.queue";
    
    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMqUtils.getChannel();
        logger.info("消费者03等待接收较长的消息");
        DeliverCallback deliverCallback = (consumerTag, message) -> {
            String msg = new String(message.getBody());
            SleepUtils.sleep(30);
            logger.info("消费者03接收到队列: {}, 的消息: {}", ACK_QUEUE_NAME, msg);
            channel.basicAck(message.getEnvelope().getDeliveryTag(), false);
        };

        int prefetchCount = 1;
        channel.basicQos(prefetchCount);
        
        boolean autoAck = false;
        /**
         * 1. 队列名称
         * 2. 消息是否自动应答 true为自动应答 false 为手动应答
         * 3. 消息成功消费的回调通知
         * 4. 消费未成功消费的回调通知
         */
        channel.basicConsume(ACK_QUEUE_NAME, autoAck, deliverCallback, (consumerTag) -> {logger.error("消费者03取消消费回调通知");});
    }
    
}
